package com.ciwong.epaper.modules.me.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.ClassMember;
import com.ciwong.epaper.modules.me.bean.Clazz;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.mobilelib.bean.UserInfoBase;
import com.ciwong.mobilelib.utils.ViewHolder;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;



/**
 * Created by neo on 2016/7/2.
 */
public class ClassMemberAdapter extends BaseAdapter{

    public  static  final  int PAGE_TYPE_CLASS_DETAIL=0;

    public  static  final  int PAGE_TYPE_CLASS_MEMBER=1;
    /**
     * 上下文对象
     */
    private Context mContext;
    private List<ClassMember> mDatalist = new ArrayList<ClassMember>();
    private  int mDisplayPageType=PAGE_TYPE_CLASS_MEMBER;


    public  ClassMemberAdapter(Context context,int pageType)
    {
        mContext=context;
        mDisplayPageType=pageType;
    }

    @Override
    public int getCount( ) {
        if (mDisplayPageType == PAGE_TYPE_CLASS_MEMBER)
        {
            return mDatalist.size();
        }else {
            if (mDatalist.size() <= 5) {
                return mDatalist.size();
            }else {
                //班级详情页面显示5个成员
                return  5;
            }
        }
    }

    @Override
    public Object getItem(int position) {
        return mDatalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v=null;
        if (convertView == null)
        {
            if (mDisplayPageType == PAGE_TYPE_CLASS_MEMBER) {
                v = View.inflate(mContext, R.layout.item_class_member, null);
            }else {
                v = View.inflate(mContext, R.layout.item_class_detail_member, null);
            }
        }
        else
        {
            v = convertView;
        }
        //在班级成员页面显示班级成员
        if (mDisplayPageType == PAGE_TYPE_CLASS_MEMBER)
        {
            View item_class_member_left_split_line = ViewHolder.get(v, R.id.item_class_member_left_split_line);
            ImageView iv_user_avatar=ViewHolder.get(v, R.id.iv_user_avatar);
            TextView tv_user_name=ViewHolder.get(v, R.id.tv_user_name);
            if (position % 3 == 0) {
                item_class_member_left_split_line.setVisibility(View.INVISIBLE);
            } else {
                item_class_member_left_split_line.setVisibility(View.VISIBLE);
            }
            if (position < mDatalist.size()) {
                ClassMember userInfoBase = mDatalist.get(position);
                String userAvatar = userInfoBase.getAvatar();
                tv_user_name.setText(userInfoBase.getUserName());
                userAvatar = TextUtils.isEmpty(userAvatar) ? "drawable://" + R.drawable.about_icon
                        : userAvatar;
                ImageLoader.getInstance().displayImage(userAvatar, iv_user_avatar,
                        EConstants.getDefAvatarCircleBuilder());
            }
        }else {
            //在班级详情页面显示班级成员
            ImageView iv_user_avatar=ViewHolder.get(v, R.id.iv_user_avatar);
            if (position < mDatalist.size()) {
                ClassMember userInfoBase = mDatalist.get(position);
                String userAvatar = userInfoBase.getAvatar();
                userAvatar = TextUtils.isEmpty(userAvatar) ? "drawable://" + R.drawable.about_icon
                        : userAvatar;
                ImageLoader.getInstance().displayImage(userAvatar, iv_user_avatar,
                        EConstants.getDefAvatarCircleBuilder());
            }
        }

        return v;
    }


    public void clear()
    {
        if (this.mDatalist != null || !this.mDatalist.isEmpty())
        {
            this.mDatalist.clear();
            notifyDataSetChanged();
        }
    }

    public void addAll(List<ClassMember> userInfoBaseList)
    {
        if (this.mDatalist != null)
        {

            this.mDatalist.addAll(userInfoBaseList);
            notifyDataSetChanged();
        }
    }
}
